﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;


namespace CommonGame
{
    class PManager
    {
        private readonly List<Player> _players;

        public PManager()
        {
            _players = new List<Player>();
        }

        public bool CreatePlayer(String name, String owner)
        {
            var nPl = new Player(name,owner) {Items = null, Lvl = 0, NextLvl = 1};
            if (_players != null) _players.Add(nPl);

            SavePlayers();
            return true;
        }

        public bool DeletePlayer(String owner)
        {
            return true;
        }

        public bool LoadPlayers()
        {
            String pPath = helper.paths.getPlayersDataPath();
            if (_players != null)
                foreach (var player in _players)
                {
                    if (!Directory.Exists(pPath + "\\" + player.Name))
                        Directory.CreateDirectory(pPath + "\\" + player.Name);
                    if (!File.Exists(string.Format("{0}\\{1}\\stat.xml", pPath, player.Name)))
                        return false;
                    var doc = new XmlDocument();
                    StreamReader f = File.OpenText(string.Format("{0}\\{1}\\stat.xml", pPath, player.Name));
                    string str = f.ReadToEnd();
                    f.Close();

                    doc.LoadXml(str);
                    string name = doc.ChildNodes[0].ChildNodes[0].Attributes.GetNamedItem("name").Value;
                    string owner = doc.ChildNodes[0].ChildNodes[0].Attributes.GetNamedItem("owner").Value;
                    int lvl = Convert.ToInt32(doc.ChildNodes[0].ChildNodes[0].Attributes.GetNamedItem("lvl").Value);
                    int nextlvl = Convert.ToInt32(doc.ChildNodes[0].ChildNodes[0].Attributes.GetNamedItem("nextlvl").Value);

                    var pl = new Player(name,owner) {Lvl = lvl, NextLvl = nextlvl};
                    _players.Add(pl);
                }
            return true;
        }

        public bool SavePlayers()
        {
            if (_players == null)
                return false;
            String pPath = helper.paths.getPlayersDataPath();
            foreach (var player in _players)
            {
                if (!Directory.Exists(pPath + "\\" + player.Name))
                    Directory.CreateDirectory(pPath + "\\" + player.Name);
                var doc = new XmlDocument();
                XmlNode root = doc.CreateElement("Player");
                doc.AppendChild(root);
                XmlNode stat = doc.CreateElement("Stat");
                root.AppendChild(stat);
                XmlAttribute name = doc.CreateAttribute("name");
                name.Value = player.Name;
                stat.Attributes.Append(name);
                XmlAttribute lvl = doc.CreateAttribute("lvl");
                lvl.Value = player.Lvl.ToString();
                stat.Attributes.Append(lvl);
                XmlAttribute owner = doc.CreateAttribute("owner");
                owner.Value = player.Owner;
                stat.Attributes.Append(owner);
                XmlAttribute nextlvl = doc.CreateAttribute("nextlvl");
                nextlvl.Value = player.NextLvl.ToString();
                stat.Attributes.Append(nextlvl);
                doc.Save(string.Format("{0}\\{1}\\stat.xml", pPath, player.Name));
            }
            return true;
        }
    }
}
